package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.MemberInfoParamVerify;
import com.sojson.project.entity.po.MemberInfo;
import com.sojson.project.service.MemberInfoService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;

/**
 * 企业资料认证表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "企业资料认证表管理")
@RestController
@RequestMapping(value = "/memberInfo")
public class MemberInfoController {

    @Resource
    private MemberInfoService memberInfoServiceImpl;

    /**
     * 单条插入
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insert")
    public Object insert(@RequestBody MemberInfo memberInfo) throws SQLException {
        memberInfoServiceImpl.insert(memberInfo);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.insertList(memberInfo));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.deleteByDto(memberInfo));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = memberInfoServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<MemberInfo> findAll = memberInfoServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody MemberInfo memberInfo) throws SQLException {
        int countByDto = memberInfoServiceImpl.countByDto(memberInfo);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/memberInfo/findByDto')")
    public Object findByDto(MemberInfo memberInfo) throws SQLException {
        List<MemberInfo> findByDto = memberInfoServiceImpl.findByDto(memberInfo);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        memberInfoServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(memberInfoServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody MemberInfo memberInfo) throws SQLException {
        memberInfoServiceImpl.updateById(memberInfo);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.updateStaticByIdList(memberInfo));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.updateChangeByIdList(memberInfo));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PutMapping(value = "/updatePartialById")
    public Object updatePartialById(@RequestBody MemberInfo memberInfo) throws SQLException {
        // 校验参数
        Result<?> paramResult = MemberInfoParamVerify.updatePartialById(memberInfo);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        memberInfo.updateInit();
        memberInfo.setUpdateNull();
        memberInfoServiceImpl.updatePartialById(memberInfo);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.updateStaticPartialByIdList(memberInfo));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody MemberInfo memberInfo) throws SQLException {
        return Result.success(memberInfoServiceImpl.updateChangePartialByIdList(memberInfo));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findById")
    @PreAuthorize("@ss.login()")
    public Object findById(Integer id) throws SQLException {
        MemberInfo findById = memberInfoServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<MemberInfo> findByIdList = memberInfoServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody MemberInfo memberInfo) throws SQLException {
        ArrayList<Integer> findIdByDto = memberInfoServiceImpl.findIdByDto(memberInfo);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        memberInfoServiceImpl.clear();
        return Result.success();
    }

    /**
     * 审核企业认证信息
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/audit")
    @PreAuthorize("@ss.hasPermi('/memberInfo/audit')")
    public Object audit(@RequestBody MemberInfo memberInfo) throws SQLException {
        // 校验参数
        Result<?> paramResult = MemberInfoParamVerify.audit(memberInfo);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        memberInfoServiceImpl.audit(memberInfo);
        return Result.success();
    }

    /**
     * 通过mId查询
     * 
     * @param mId
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByMId")
    @PreAuthorize("@ss.login()")
    public Object findByMId(String mId) throws SQLException {
        MemberInfo findByMId = memberInfoServiceImpl.findByMId(mId);
        return Result.success(findByMId);
    }

    /**
     * 通过mid更新(局部更新)
     * 
     * @param memberInfo
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialByMId")
    @PreAuthorize("@ss.login()")
    public Object updatePartialByMId(@RequestBody MemberInfo memberInfo) throws SQLException {
        // 校验参数
        Result<?> paramResult = MemberInfoParamVerify.updatePartialByMId(memberInfo);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        memberInfo.updateInit();
        memberInfo.setId(null);
        memberInfoServiceImpl.updatePartialByMId(memberInfo);
        return Result.success();
    }

}